package org.xfg.domain.strategy.service.raffle;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import org.xfg.domain.active.model.valobj.RuleWeightVO;
import org.xfg.domain.strategy.model.valobj.RuleTreeVO;
import org.xfg.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import org.xfg.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.xfg.domain.strategy.repository.IStrategyRepository;
import org.xfg.domain.strategy.service.AbstractRaffleStrategy;
import org.xfg.domain.strategy.service.armory.IStrategyDispatch;
import org.xfg.domain.strategy.service.rule.chain.ILogicChain;
import org.xfg.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.xfg.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.xfg.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy {

    public DefaultRaffleStrategy(IStrategyRepository repository, IStrategyDispatch strategyDispatch, DefaultChainFactory defaultChainFactory, DefaultTreeFactory defaultTreeFactory) {
        super(repository, strategyDispatch, defaultChainFactory, defaultTreeFactory);
    }

    /**
     * 实现链式抽奖前置过滤
     **/
    @Override
    public DefaultChainFactory.StrategyAwardVO raffleLogicChain(String userId, Long strategyId) {
        ILogicChain logicChain = defaultChainFactory.openLogicChain(strategyId);
        return logicChain.logic(userId, strategyId);
    }

    /**
     *
     * 树节点过滤规则
     */
    @Override
    public DefaultTreeFactory.StrategyAwardVO raffleLogicTree(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        // 查询奖品策略对应的规则
        StrategyAwardRuleModelVO strategyAwardRuleModelVO = repository.queryStrategyAwardRuleModelVO(strategyId, awardId);
        // 没有规则配置直接返回奖品无需过滤处理
        if (null == strategyAwardRuleModelVO){
            return DefaultTreeFactory.StrategyAwardVO.builder().awardId(awardId).build();
        }
        // 查询规则树
        RuleTreeVO ruleTreeVO = repository.queryRuleTreeVOByTreeId(strategyAwardRuleModelVO.getRuleModels());
        // 奖品配置规则,但是查询不到具体的规则树信息则报错
        if (null == ruleTreeVO){
            throw new RuntimeException("存在抽奖策略配置的规则模型 Key，未在库表 rule_tree、rule_tree_node、rule_tree_line 配置对应的规则树信息 " + strategyAwardRuleModelVO.getRuleModels());
        }
        IDecisionTreeEngine treeEngine = defaultTreeFactory.openLogicTree(ruleTreeVO);

        return treeEngine.process(userId, strategyId, awardId,endDateTime);
    }

    // 取出队列中需要同步数据库的数据
    @Override
    public StrategyAwardStockKeyVO takeQueueValue() throws InterruptedException {
        return repository.takeQueueValue();
    }

    // 修改奖品库存
    @Override
    public void updateStrategyAwardStock(Long strategyId, Integer awardId) {
        repository.updateStrategyAwardStock(strategyId, awardId);
    }

    @Override
    public List<RuleWeightVO> queryAwardRuleWeight(Long strategyId) {
        return repository.queryAwardRuleWeight(strategyId);
    }
    @Override
    public List<RuleWeightVO> queryAwardRuleWeightByActivityId(Long activityId) {
        Long strategyId = repository.queryStrategyIdByActivityId(activityId);
        return  queryAwardRuleWeight(strategyId);
    }
}

















